<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>标注和标注图层-海量点</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <style>
        html,
        body,
        #container {
            height: 100%;
            width: 100%;
            margin: 0;
        }

        .amap-info-window {
            width: 150px;
            background: #fff;
            border-radius: 3px;
            padding: 3px 7px;
            box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
            position: relative;
        }

        .amap-info-sharp {
            position: absolute;
            top: 21px;
            bottom: 0;
            left: 50%;
            margin-left: -8px;
            border-left: 8px solid transparent;
            border-right: 8px solid transparent;
            border-top: 8px solid #fff;
        }

        .collision-btn.disable {
            background-image: none;
            color: lightgrey;
            border-color: lightgrey;
            cursor: not-allowed;
        }

        .collision-btn.disable:hover {
            background-color: #fff;
            border-color: lightgrey;
        }

        .input-card .btn {
            margin-right: .8rem;
        }
    </style>
</head>

<body>
    <div id="container"></div>
    <div class="input-card">
        <label style="color:grey">设置</label>
        <div class="input-item">
            <input id="allowCollision" type="button" class="btn collision-btn" onclick="click1()" value="添加">
            <input id="notAllowCollision" type="button" class="btn collision-btn" onclick="click2()" value="移除">
        </div>
    </div>
    <div class="info">
        海量点展示: 30000
    </div>
    <script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=2117aeb766c99a2763b91b68ce2530e8"></script>
    <script type="text/javascript"
        src="https://a.amap.com/jsapi_demos/static/demo-center/data/mock_position.js"></script>
    <script type="text/javascript">
        var map = new AMap.Map('container', {
            zoom: 9,
            viewMode: '3D',
            center: [116.12, 40.11],
            mapStyle: 'amap://styles/whitesmoke',
            showLabel: false,
            showIndoorMap: false,
        });

        // map.on('complete', function () {
        // 创建 AMap.LabelsLayer 图层
        var layer = new AMap.LabelsLayer({
            zooms: [3, 20],
            zIndex: 1000,
            collision: false
        });

        // 将图层添加到地图
        map.add(layer);

        var markers = [];
        var positions = Positions.slice(0, 3E4);

        var icon1 = {
            type: 'image',
            image: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',
            size: [6, 9],
            anchor: 'bottom-center',
        };

        for (var i = 0; i < positions.length; i++) {
            var curPosition = positions[i];
            var curData = {
                position: curPosition,
                icon: icon1,
                text: {
                    content: curPosition,
                }
            };

            var labelMarker = new AMap.LabelMarker(curData);

            markers.push(labelMarker);

            // 给marker绑定事件
            // labelMarker.on('mouseover', function (e) {
            //     var position = e.data.data && e.data.data.position;

            //     if (position) {
            //         normalMarker.setContent(
            //             '<div class="amap-info-window">'
            //             + position +
            //             '<div class="amap-info-sharp"></div>' +
            //             '</div>');
            //         normalMarker.setPosition(position);
            //         map.add(normalMarker);
            //     }
            // });

            // labelMarker.on('mouseout', function () {
            //     map.remove(normalMarker);
            // });
        }

        // 普通点
        var normalMarker = new AMap.Marker({
            anchor: 'bottom-center',
            offset: [0, -15],
        });
        // });

        // 一次性将海量点添加到图层
        function click1() {
            layer.add(markers);
        }
        function click2() {
            layer.remove(markers);
        }
    </script>
</body>

</html>